home *** CD-ROM | disk | FTP | other *** search
/ Linux Cubed Series 8: LINUX Games / Linux Cubed Series 8 - LINUX Games.iso / games / video / fly8111-.000 / fly8111- / fly8 / gear.c < prev    next >
C/C++ Source or Header  |  1979-12-31  |  6KB  |  245 lines

  1. /* --------------------------------- gear.c --------------------------------- */
  2.  
  3. /* This is part of the flight simulator 'fly8'.
  4.  * Author: Eyal Lebedinsky (eyal@ise.canberra.edu.au).
  5. */
  6.  
  7. /* Account for landing gear effects.
  8.  *
  9.  * Not sure how it works, but I think that once I add the ground contact as
  10.  * a series of forces (one for each contact) then I get:
  11.  *
  12.  * P0 = center of gravity, [0,0,0] in body axes.
  13.  * P1 = contact point
  14.  * F  = force at P1
  15.  * P  = P1-P0, P1 in body axes
  16.  *
  17.  * Now a vector from F1=F*P is the component of force that operates on the
  18.  * cg and generates body forces while the rest F2=F-F1 at a distance of |P|
  19.  * is the moment.
  20. */
  21.  
  22. #include "plane.h"
  23.  
  24.  
  25. #define HEIGHT        EX->misc[6]
  26. #define MAXFORCE    FCON(0.05)
  27.  
  28. static int    sx, cx, sy, cy, sz, cz, cxcy;
  29. static MAT    FM;
  30.  
  31. LOCAL_FUNC void NEAR
  32. GearCalcs (OBJECT *p, struct gear *g, VECT MM, VECT F, int flag)
  33. {
  34.     VECT    GFe, GFb, GMb;
  35.     int    t, dg, ddg, vfact;
  36.     int    Ux, Uy, Vx, Vy, Vt, Fu, Fs, ce, se;
  37.     ANGLE    e, gamma;
  38.  
  39. /* Find gear deflection.
  40. */
  41.     dg = fdiv (-HEIGHT -fmul (g->y, sx) +fmul (g->x, sy), cxcy) -g->z;
  42.     if (dg <= 0) {
  43.         EX->equip &= ~flag;
  44.         return;            /* no ground contact */
  45.     } else
  46.         EX->equip |= flag;
  47.  
  48. /* Find gear upwards force. If the gear deflection is less than the tyre
  49.  * deflection then we have force (Ft) linear with the deflection. After
  50.  * this point the precharge force (P) is exceeded and the force (Fs) is
  51.  * linear with the reciplrocal of the strut length ( when the strut is at
  52.  * half length the force is 2P, at 1/3rd length it is 3P etc.). The strut
  53.  * has a damping force (Fd) linear with the deflection rate (ddg) and in
  54.  * reverse direction.
  55. */
  56.     if (dg <= g->dtp)        /* only tyre deflection */
  57.         GFe[Z] = muldiv (g->P, dg, g->dtp);
  58.     else {
  59.         t = fmul ((int)(FONE/(VONE*C_PI/2)), cxcy);
  60.         ddg = - fdiv (p->V[Z]*VONE, cxcy)
  61.               - muldiv (fmul (g->y, cx), p->dae[X], t)
  62.               + muldiv (fmul (g->x, cy), p->dae[Y], t);
  63.  
  64.         if (dg > g->dgmax)
  65.             t = MAXFORCE;
  66.         else {
  67.             t = fdiv (g->dgmax-dg+g->dtp, g->dgmax);
  68.             if (t < MAXFORCE)
  69.                 t = MAXFORCE;
  70.         }
  71.  
  72.         GFe[Z] = fdiv (g->P, t) + muldiv (ddg, g->Cv, VONE*VONE);
  73.         if (GFe[Z] < 0)
  74.             GFe[Z] = 0;
  75.     }
  76.  
  77. /* Get basic friction coefficients. In reality one should check for the
  78.  * wheel being locked (and slipping) and apply the relevant co. but we
  79.  * assume it does not happen.
  80. */
  81.     Ux = g->us;
  82.     Uy = muldiv (EX->brake, g->ub, 100) + g->ur;
  83.  
  84. /* Find what velocity the tyre actually sees.
  85.  * 'vfact' allows accounting for angular velocity when the linear velocity
  86.  * is small.
  87. */
  88.     vfact = p->speed < 100*VONE ? VONE : 1;
  89.     t = (int)(FONE/(C_PI/2))/vfact;
  90.     Vx = EX->v[X]*vfact - muldiv (g->y, p->da[Z], t);
  91.     Vy = EX->v[Y]*vfact + muldiv (g->x, p->da[Z], t);
  92.  
  93. /* 'gamma' is the angle (relative to the plane forward axis) of the tyre
  94.  * velocity.
  95. */
  96.     gamma = ATAN(Vx, iabs(Vy));
  97.     Vt = ihypot2d (Vx, Vy);
  98.     if (Vy < 0)
  99.         Vt = -Vt;
  100.  
  101. /* These are the basic friction forces.
  102. */
  103.     Fs = -fmul (GFe[Z], Ux);
  104.     Fu = -fmul (GFe[Z], Uy);
  105.  
  106.     if (g->emax && EX->rudder) {
  107.  
  108. /* This tyre is steering. Find the steering angle e. Pedals effectiveness
  109.  * is reduced with speed.
  110. */
  111.         if (EP->APrate && p->speed > EP->APrate)
  112.             t = muldiv (EX->rudder, EP->APrate, p->speed);
  113.         else
  114.             t = EX->rudder;
  115.  
  116.         e = -muldiv (t, g->emax, 100);
  117.         ce = COS(e);
  118.         se = SIN(e);
  119.  
  120. /* Find the velocities in tyre coordinates.
  121. */
  122.         t  = fmul (Vx, ce) - fmul (Vy, se);
  123.         Vy = fmul (Vy, ce) + fmul (Vx, se);
  124.         Vx = t;
  125.  
  126.         Fs = Vx ? (Vx<0 ? -Fs : Fs) : 0;
  127.         Fu = Vy ? (Vy<0 ? -Fu : Fu) : 0;
  128.  
  129. /* Now we assume that the side force is dependent on the difference
  130.  * between the desired rolling angle (e) and the actual rolling angle
  131.  * (gamma) and is linear with the forward velocity and the effective
  132.  * load on the tyre. We do not allow for slip here.
  133. */
  134.         t = fmul (GFe[Z], Ux);
  135.         Fs += muldiv (fmul (t, SIN (e-gamma)), Vy, VONE*vfact);
  136.  
  137. /* Now we relate the tyre forces back to body axes.
  138. */
  139.         GFe[X] = fmul (Fs, ce) + fmul (Fu, se);
  140.         GFe[Y] = fmul (Fu, ce) - fmul (Fs, se);
  141.     } else {
  142.         GFe[X] = Vx ? (Vx<0 ? -Fs : Fs) : 0;
  143.         GFe[Y] = Vy ? (Vy<0 ? -Fu : Fu) : 0;
  144.     }
  145.  
  146.     VMmul (GFb, GFe, FM);
  147.     Vinc (F, GFb);
  148.  
  149. /* get moments.
  150. */
  151.  
  152. /*
  153.     .Y    .X    .Z
  154.  
  155. Y.     0    -z     x
  156. X.     z     0    -y
  157. Z.    -x     y     0
  158.  
  159. */
  160.     t = g->z + dg;
  161.  
  162. /* The units involved are:
  163.  *
  164.  * g->x        *VONE*VONE
  165.  * F[]        *VONE/W
  166.  * M[]        *2/PI/VONE/VONE/W    [frac]
  167.  *
  168.  * So we need the following factor for unit conversion.
  169.  * The trailing '*4' is a fudge factor...
  170. */
  171. #define VVV    (int)(VONE*VONE*C_PI/2*VONE*VONE*VONE/FONE)*4
  172.  
  173.     GMb[X] = -muldiv (GFb[Y], t,    VVV) + muldiv (GFb[Z], g->y, VVV);
  174.     GMb[Y] =  muldiv (GFb[X], t,    VVV) - muldiv (GFb[Z], g->x, VVV);
  175.     GMb[Z] = -muldiv (GFb[X], g->y, VVV) + muldiv (GFb[Y], g->x, VVV);
  176. #undef VVV
  177.  
  178.     Vinc (MM, GMb);
  179. }
  180.  
  181. /* Landing gear action.
  182. */
  183. extern void FAR
  184. LandGear (OBJECT *p, VECT F, VECT MM)
  185. {
  186.     int    t;
  187.     Ushort    flag;
  188.  
  189. /* landing gear buffeting
  190. */
  191.     if (EX->equip & EQ_GEAR) {
  192.         t = ~1 & (p->speed/256);
  193.         t = t*t;
  194.         F[X] += Frand()%(1+2*t) - t;
  195.         F[Y] -= (Frand()%(1+2*t))/64;
  196.         F[Z] += Frand()%(1+2*t) - t;
  197.     }
  198.  
  199.     if (!(EX->flags & PF_ONGROUND))
  200.         return;
  201.  
  202.     HEIGHT += TADJ(p->V[Z]*VONE);
  203.     p->R[Z] = HEIGHT/VONE;
  204.  
  205.     cx = p->cosx;
  206.     cy = p->cosy;
  207.  
  208.     if (cx < FCON(0.5) || cy < FCON(0.5))
  209.         return;                /* no ground contact */
  210.  
  211.     sx = p->sinx;
  212.     sy = p->siny;
  213.  
  214.     sz = p->sinz;
  215.     cz = p->cosz;
  216.  
  217.     cxcy = fmul (cx, cy);
  218.  
  219. /*
  220.      X.     Y.     Z.
  221.  
  222. .X     cy     sx*sy    -cx*sy
  223. .Y     0     cx     sx
  224. .Z     sy    -sx*cy     cx*cy
  225.  
  226. */
  227.     FM[X][X] =  cy;
  228.     FM[X][Y] =  0;
  229.     FM[X][Z] =  sy;
  230.     FM[Y][X] =  fmul (sx, sy);
  231.     FM[Y][Y] =  cx;
  232.     FM[Y][Z] = -fmul (sx, cy);
  233.     FM[Z][X] = -fmul (cx, sy);
  234.     FM[Z][Y] =  sx;
  235.     FM[Z][Z] =  cxcy;
  236.  
  237.     for (flag = EQ_GEAR1, t = 0; t < rangeof(EP->gear) && EP->gear[t].z;
  238.                                     ++t) {
  239.         GearCalcs (p, &EP->gear[t], MM, F, flag);
  240.         flag <<= 1;
  241.     }
  242. }
  243.  
  244. #undef HEIGHT
  245.